﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel.Syndication;
using System.Text;
using System.Threading.Tasks;

namespace Cofe.Web.Feed
{
    public class FeedEntry<T>
        where T : EventArgs
    {
        private readonly SyndicationItem _item;
        private readonly string _fileName;

        public FeedEntry(SyndicationItem item, string fileName)
        {
            _item = item;
            _fileName = fileName;
        }

        public void AddTo(SyndicationFeed feed)
        {
            ((IList<SyndicationItem>)feed.Items).Insert(0, _item);
        }

        public void Save()
        {
            using (var writer = CofeWsServices.Feeds.GetFeedService<T>()
                .FileSystem.GetXmlWriter(FeedDirectory.Entry, _fileName))
            {
                _item.GetAtom10Formatter().WriteTo(writer);
            }
        }
    }
}
